#====# Appendix G: Effect sizes on market capitalization #====#

# Load libraries and set defaults ----
library(modelsummary)
library(tinytable)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/event_function.R")
source("aux/plot_theme.R")

# Import data ----
stocks <- read_rds("data_out/stocks_analysis.rds") # main analysis dataset

# Compute capitalization gains ----
cap <- stocks %>%
  group_by(ticker_symbol) %>%
  arrange(ticker_symbol, date) %>%
  mutate(l_prccd = lag(prccd)) %>%
  filter(date == as.Date("2025-02-10")) %>%
  filter(FCPA_sample == 1) %>%
  mutate(diff = cshoq * l_prccd * abn_chg/100)

mean(cap$diff, na.rm = TRUE)
# average gain: $159,753,977

sum(cap$diff, na.rm = TRUE)
# total portfolio gain: $38,500,708,486

stocks %>% 
  filter(FCPA_sample == 1) %>% 
  select(ticker_symbol, sanction_avg) %>%
  distinct() %>%
  select(sanction_avg) %>%
  pull() %>%
  mean(na.rm = TRUE)
# The average FCPA sanction is: $154,934,267

# Table G.1: Top 50 estimated effect sizes of the FCPA Executive Order among past FCPA targets ----
cap %>%
  arrange(-diff) %>%
  select(conm, abn_chg, diff, cshoq, l_prccd) %>%
  ungroup() %>%
  mutate(diff = diff/10^6,
         cshoq = cshoq/10^6,
         Rank = row_number()) %>%
  filter(!is.na(cshoq)) %>%
  relocate(Rank) %>%
  filter(Rank <= 50) %>%
  # cosmetic edits in the cells:
  mutate(Rank = as.character(Rank),
         conm = str_replace_all(conm, "\\&", "\\\\&"),
         # format doubles so that they have a comma splitting the thousands
         across(.cols = c("abn_chg", "diff", "cshoq", "l_prccd"),
                .fns = ~str_trim(format(round(.x, 3), nsmall = 3, big.mark = ","))),
         # add a $ symbol before the price and capitalization gain:
         across(.cols = c("l_prccd", "diff"),
                .fns = ~paste0("\\$", .x)),
         # add a % symbol after the AR:
         abn_chg = paste0(abn_chg, "\\%")) %>%
  relocate(Rank, conm, ticker_symbol, cshoq, l_prccd, abn_chg, diff) %>%
  rename("Ticker" = "ticker_symbol",
         "Company name" = "conm",
         "\\textsc{ar}" = "abn_chg",
         "Capitalization gain (M)" = "diff",
         "Shares (M)" = "cshoq",
         "Price" = "l_prccd") %>%
  datasummary_df(fmt = 3, 
                 title = "Top 50 estimated effect sizes of the FCPA Executive Order among past FCPA targets \\label{tab:effect_size}") %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_G1.html", overwrite = TRUE)

# Figure G.1: Estimated market gains for past FCPA targets on the day of Trump’s FCPA Executive Order ----
stocks %>%
  group_by(ticker_symbol) %>%
  arrange(ticker_symbol, date) %>%
  mutate(l_prccd = lag(prccd)) %>%
  filter(date == as.Date("2025-02-10")) %>%
  mutate(diff = cshoq * l_prccd * abn_chg/100,
         diff = diff/10^9,
         FCPA_sample = factor(FCPA_sample, levels = c(1, 0),
                              labels = c("Past FCPA targets", 
                                         "Non-FCPA targets"))) %>%
  filter(FCPA_sample == "Past FCPA targets") %>%
  ggplot(aes(x = diff)) +
  geom_density(fill = "black", alpha = .2) +
  geom_vline(xintercept = 0, linewidth = 0.1) +
  xlab("Estimated change in market capitalization (B)") + ylab("") +
  scale_x_continuous(labels = ~paste0(.x, "$")) +
  scale_color_manual("", values = c("grey20", "grey60")) +
  scale_fill_manual("", values = c("grey20", "grey60"))
ggsave("plots/figure_G1.pdf", height = 2.5, width = 5)

#====# The End #====#